'use strict';

const Service = require('egg').Service;

class NoticeService extends Service {

  async getNoticeDetail({ noticeId }) {
    const query = { _id: noticeId };

    const notice = await this.ctx.model.Notice.findOne(query).lean();

    if (!notice) {
      return [];
    }

    return [ notice ];
  }

  async getNoticeList(query, option) {

    const totalCount = await this.ctx.model.Notice.count(query);
    const noticeList = await this.ctx.model.Notice.find(query, {}, option).exec();

    if (noticeList.length === 0) {
      return [];
    }

    return [ noticeList, totalCount ];
  }

  async newNotice({
    userId,
    title,
    content,
  }) {
    const n = new this.ctx.model.Notice();

    n.user_id = userId;
    n.title = title;
    n.content = content;
    await n.save();

    return [ n._id ];
  }
}

module.exports = NoticeService;
